package chap_7;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.*;
class Ticket{
    private int count =3;
    private Lock lock=new ReentrantLock();
    public void sale(){
        this.lock.lock();
        try{
            if(this.count>0){
                TimeUnit.SECONDS.sleep(1);
                System.out.println("["+Thread.currentThread().getName()+"]卖票,剩余票数:"+this.count--);
            }else{
                System.err.println("["+Thread.currentThread().getName()+"]票已经卖完了,明天再来!");}
            }catch (Exception e){
        }finally {
                      this.lock.unlock();
        }
    }
}

public class seven250 {
    public static void main(String[] args) throws  Exception{
        Ticket ticket = new Ticket();
        for(int x =0;x<5;x++){
            new Thread(()->{
                ticket.sale();
            },"售票员-"+x).start();
        }

    }
}
